//引入远程js，加载完成后执行相应方法
export function loadingJs2(src) {
  return new Promise((resolve,reject)=>{
    let script = document.createElement('script');
    script.type = "text/javascript";
    script.src= src;
    document.body.appendChild(script);
      
    script.onload = ()=>{
      resolve();
    }
    script.onerror = ()=>{
      reject();
    }
  })
}
 




/**
 * 动态加载js
 * @param url
 * @param callback
 * @returns {Promise<any>}
 */
export function loadingJs(url, callback) {
  return new Promise((resolve, reject) => {
    let fn = callback || function () {};
    let script = document.createElement('script');
    script.src = url;
    script.type = 'text/javascript';
    document.head.appendChild(script);
    if (script.addEventListener) {
      script.addEventListener('load', function () {
        console.log("加载完成",url);
        fn();
        resolve();
      }, false);
    } else if (script.attachEvent) {
      script.attachEvent('onreadystatechange', function () {
        let target = window.event.srcElement;
        if (target.readyState == 'loaded') {
          console.log("加载完成",url);
          fn();
          resolve();
        }
      });
    }
  })
}

/**
 * 动态加载css
 * @param href
 * @param callback
 * @returns {Promise<any>}
 */
/* export function loadingCss(href, callback) {
  return new Promise((resolve, reject) => {
    let fn = callback || function () {};
    let link = document.createElement('link');
    link.href = href;
    link.rel = 'stylesheet';
    document.head.appendChild(link);
    superviseCssLinkLoad(link, () => {  fn();resolve();console.log("加载完成",href); })
  })
} */

/**
 * 通过轮询link标签的sheet. cssRules来判断是否加载好css资源
 * @param link
 * @param onload
 */
/* function superviseCssLinkLoad(link, onload) {
  requestIdleCallback(() => {
    if (link.sheet) {
      if (link.sheet.cssRules?.length) {
        typeof onload === 'function' && onload()
      } else {
        superviseCssLinkLoad(link, onload)
      }
    } else {
      superviseCssLinkLoad(link, onload)
    }
  })
} */
